﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace CodeTalk.CTSpeechCommands
{
    class LearnType : SpeechCommand
    {
        internal override GrammarBuilder GrammarBld(Context context, Context[] libraries)
        {
            return new GrammarBuilder("learn type");
        }

        public override string Name
        {
            get { return "LearnType"; }
        }

        public override string Description
        {
            get { return "Reads the currently selected text, builds a type from it and commits this type into the current Context. "; }
        }

        internal override bool CanSpokenFormMatch(string spokenForm)
        {
            return spokenForm == "learn type";
        }

        internal override object Execute(SpeechMode callingMode, string recognizedText, SemanticValue semValue)
        {
            string selectedText = InputHandler.ReadSelectedText();
            if (selectedText != null)
            {
                selectedText = selectedText.Trim();
                SymbolCategory? category = CodeSymbol.DetermineCategory(selectedText);
                if (category.HasValue)
                {
                    if (category.Value == SymbolCategory.Function)
                    {
                        callingMode.CurrentGuiHandler.SetErrorFeedback("Selected text is not a valid type!");
                        return null;
                    }
                    string pronounceable = TextUtilz.MakePronounceable(selectedText).ToLowerInvariant();
                    string errMsg = "";
                    uint id = callingMode.CurrentContext.CreateNewSymbol(selectedText, pronounceable, SymbolCategory.Type, callingMode, ref errMsg);
                    if (id == 0)
                        callingMode.CurrentGuiHandler.SetErrorFeedback("Could not create new type: " + errMsg);
                    else
                    {
                        callingMode.CurrentGuiHandler.SetPrimaryFeedback("Type learned: " + selectedText + " (" + pronounceable + ")");
                        callingMode.RecompileGrammar();
                        callingMode.CurrentGuiHandler.RefreshSymbolView();
                        return id;
                    }
                }
                else
                {
                    callingMode.CurrentGuiHandler.SetErrorFeedback("Selected text is not a valid type!");
                }
            }
            else
            {
                callingMode.CurrentGuiHandler.SetErrorFeedback("No valid text selected to learn!");
            }
            return null;
        }

        internal override void Undo(SpeechMode callingMode, string recognizedText, SemanticValue semValue, object undoObject)
        {
            if (undoObject != null)
            {
                uint id = (uint)undoObject;
                callingMode.CurrentContext.DeleteSymbolWithID(id);
                callingMode.CurrentGuiHandler.RefreshSymbolView();
            }
        }
    }
}
